using UnityEngine;

[ExecuteInEditMode]
public class ControllerPlatformMobile : ControllerGeneric
{
	private float unit = 0.5f;

	public float length = 1f;

	public float width = 1f;

	public GameObject cube;

	public Object controllerStartPrefab;

	public Object controllerFinishPrefab;

	public GameObject start;

	public GameObject finish;

	public float startDelay;

	public float speed = 1f;

	public float pause;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		SetNames();
		SetParenting();
		PlatformMobile platformMobile = cube.AddComponent<PlatformMobile>();
		platformMobile.startPositionOffset = start.transform.position - base.transform.position;
		platformMobile.finishPositionOffset = finish.transform.position - base.transform.position;
		platformMobile.delay = startDelay;
		platformMobile.pause = pause;
		platformMobile.speed = speed;
		base.name = "will_destroy_" + base.name;
		if (Application.isPlaying)
		{
			Object.Destroy(start);
			Object.Destroy(finish);
			Object.Destroy(base.gameObject);
		}
		else
		{
			Object.DestroyImmediate(start);
			Object.DestroyImmediate(finish);
			Object.DestroyImmediate(base.gameObject);
		}
	}

	private void Update()
	{
		if (!Application.isEditor || Application.isPlaying)
		{
			return;
		}
		SetupRemoteControl();
		if (IsRemoteControlActive())
		{
			base.transform.position = remoteControl.transform.position;
			base.transform.rotation = remoteControl.transform.rotation;
			length = remoteControl.transform.localScale.z;
			width = remoteControl.transform.localScale.x;
		}
		if (length < 0.0001f)
		{
			length = 0.0001f;
		}
		if (width < 0.0001f)
		{
			width = 0.0001f;
		}
		if (remoteControl != null)
		{
			if (remoteControl.manualScale)
			{
				remoteControl.scale = new Vector3(remoteControl.scale.x, unit, remoteControl.scale.z);
			}
			else
			{
				remoteControl.transform.localScale = new Vector3(width, unit, length);
			}
		}
		Setup();
	}

	private void Setup()
	{
		AssureParenting();
		cube.transform.localPosition = new Vector3(0f, (0f - ControllerGeneric.floorHeight) * 0.5f, 0f);
		cube.transform.localEulerAngles = Vector3.zero;
		cube.transform.localScale = new Vector3(width, ControllerGeneric.floorHeight / 0.25f, length);
		SetupUVs();
	}

	private void SetParenting()
	{
		for (int childCount = base.transform.childCount; childCount > 0; childCount = base.transform.childCount)
		{
			base.transform.GetChild(0).parent = base.transform.parent;
		}
	}

	private void SetNames()
	{
		cube.name = "Platform Mobile";
	}

	public void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(width, unit, length);
			PresetsPlatformMobile presetsPlatformMobile = remoteControl.gameObject.AddComponent<PresetsPlatformMobile>();
			presetsPlatformMobile.SetupControllerStart(controllerStartPrefab);
			presetsPlatformMobile.SetupControllerFinish(controllerFinishPrefab);
			presetsPlatformMobile.controllerStart.remoteControl.transform.position = base.transform.position + Vector3.back * length;
			remoteControl.SetPlacementParent(presetsPlatformMobile.controllerStart.remoteControl);
			remoteControl.alignX = BoxAlignment.Center;
			remoteControl.alignY = BoxAlignment.RightIn;
			remoteControl.alignZ = BoxAlignment.RightOut;
			presetsPlatformMobile.controllerFinish.remoteControl.SetPlacementParent(presetsPlatformMobile.controllerStart.remoteControl);
			presetsPlatformMobile.controllerFinish.remoteControl.alignX = BoxAlignment.Center;
			presetsPlatformMobile.controllerFinish.remoteControl.alignY = BoxAlignment.RightIn;
			presetsPlatformMobile.controllerFinish.remoteControl.alignZ = BoxAlignment.RightOut;
			presetsPlatformMobile.controllerFinish.remoteControl.alignZOffset = length;
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		ControllerPlatformMobile component = rc.controller.GetComponent<ControllerPlatformMobile>();
		PresetsPlatformMobile component2 = rc.gameObject.GetComponent<PresetsPlatformMobile>();
		PresetsPlatformMobile component3 = remoteControl.gameObject.GetComponent<PresetsPlatformMobile>();
		component3.controllerFinish.remoteControl.alignXOffset = 0f;
		component3.controllerFinish.remoteControl.alignYOffset = 0f;
		component3.controllerFinish.remoteControl.alignZOffset = 0f;
		remoteControl.alignX = BoxAlignment.None;
		remoteControl.alignY = BoxAlignment.None;
		remoteControl.alignZ = BoxAlignment.None;
		component3.controllerFinish.remoteControl.alignX = BoxAlignment.None;
		component3.controllerFinish.remoteControl.alignY = BoxAlignment.None;
		component3.controllerFinish.remoteControl.alignZ = BoxAlignment.None;
		component3.SetPositionControllerStart(component.start.transform.position);
		component3.SetPositionControllerFinish(component.finish.transform.position);
		remoteControl.SetPosition(rc.transform.position);
		component3.controllerStart.remoteControl.SetPlacementParent(component2.controllerStart.remoteControl.placementParent);
		component3.controllerStart.remoteControl.alignX = component2.controllerStart.remoteControl.alignX;
		component3.controllerStart.remoteControl.alignY = component2.controllerStart.remoteControl.alignY;
		component3.controllerStart.remoteControl.alignZ = component2.controllerStart.remoteControl.alignZ;
		component3.controllerStart.remoteControl.alignXOffset = component2.controllerStart.remoteControl.alignXOffset;
		component3.controllerStart.remoteControl.alignYOffset = component2.controllerStart.remoteControl.alignYOffset;
		component3.controllerStart.remoteControl.alignZOffset = component2.controllerStart.remoteControl.alignZOffset;
		if (component.remoteControl.placementParent == component2.controllerStart.remoteControl)
		{
			remoteControl.SetPlacementParent(component3.controllerStart.remoteControl);
		}
		else
		{
			remoteControl.SetPlacementParent(component.remoteControl.placementParent);
		}
		remoteControl.alignX = component.remoteControl.alignX;
		remoteControl.alignY = component.remoteControl.alignY;
		remoteControl.alignZ = component.remoteControl.alignZ;
		remoteControl.alignXOffset = component.remoteControl.alignXOffset;
		remoteControl.alignYOffset = component.remoteControl.alignYOffset;
		remoteControl.alignZOffset = component.remoteControl.alignZOffset;
		if (component2.controllerFinish.remoteControl.placementParent == component2.controllerStart.remoteControl)
		{
			component3.controllerFinish.remoteControl.SetPlacementParent(component3.controllerStart.remoteControl);
		}
		else
		{
			component3.controllerFinish.remoteControl.SetPlacementParent(component2.controllerFinish.remoteControl.placementParent);
		}
		component3.controllerFinish.remoteControl.alignX = component2.controllerFinish.remoteControl.alignX;
		component3.controllerFinish.remoteControl.alignY = component2.controllerFinish.remoteControl.alignY;
		component3.controllerFinish.remoteControl.alignZ = component2.controllerFinish.remoteControl.alignZ;
		component3.controllerFinish.remoteControl.alignXOffset = component2.controllerFinish.remoteControl.alignXOffset;
		component3.controllerFinish.remoteControl.alignYOffset = component2.controllerFinish.remoteControl.alignYOffset;
		component3.controllerFinish.remoteControl.alignZOffset = component2.controllerFinish.remoteControl.alignZOffset;
		component3.startDelay = component.startDelay;
		component3.speed = component.speed;
		component3.pause = component.pause;
	}

	public void SetupUVs()
	{
		Mesh mesh = cube.GetComponent<MeshFilter>().mesh;
		Vector2[] uv = mesh.uv;
		float num = 0.125f;
		float num2 = 0.125f;
		uv[4].x = 0.5f + width * num;
		uv[6].x = 0.5f - width * num;
		uv[5].x = 0.5f - width * num;
		uv[7].x = 0.5f + width * num;
		uv[4].y = 0.5f + length * num2;
		uv[6].y = 0.5f + length * num2;
		uv[5].y = 0.5f - length * num2;
		uv[7].y = 0.5f - length * num2;
		uv[0].x = 0.5f - length * num;
		uv[0].y = 0.5f - width * num2;
		uv[1].x = 0.5f + length * num;
		uv[1].y = 0.5f + width * num2;
		uv[2].x = 0.5f - length * num;
		uv[2].y = 0.5f + width * num2;
		uv[3].x = 0.5f + length * num;
		uv[3].y = 0.5f - width * num2;
		num = 0.25f;
		num2 = 0.5f;
		uv[12].x = 0.5f - width * num;
		uv[14].x = 0.5f + width * num;
		uv[13].x = 0.5f + width * num;
		uv[15].x = 0.5f - width * num;
		uv[12].y = 0.5f - num2;
		uv[14].y = 0.5f - num2;
		uv[13].y = 0.5f + num2;
		uv[15].y = 0.5f + num2;
		uv[8].x = 0.5f + width * num;
		uv[10].x = 0.5f - width * num;
		uv[9].x = 0.5f - width * num;
		uv[11].x = 0.5f + width * num;
		uv[8].y = 0.5f - num2;
		uv[10].y = 0.5f - num2;
		uv[9].y = 0.5f + num2;
		uv[11].y = 0.5f + num2;
		uv[16].x = 0.5f + length * num;
		uv[18].x = 0.5f - length * num;
		uv[17].x = 0.5f - length * num;
		uv[19].x = 0.5f + length * num;
		uv[16].y = 0.5f - num2;
		uv[18].y = 0.5f - num2;
		uv[17].y = 0.5f + num2;
		uv[19].y = 0.5f + num2;
		uv[20].x = 0.5f - length * num;
		uv[22].x = 0.5f + length * num;
		uv[21].x = 0.5f + length * num;
		uv[23].x = 0.5f - length * num;
		uv[20].y = 0.5f - num2;
		uv[22].y = 0.5f - num2;
		uv[21].y = 0.5f + num2;
		uv[23].y = 0.5f + num2;
		mesh.uv = uv;
	}
}
